// pages/homePage/homePage.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {

    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    productType: []
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.dialog = this.selectComponent("#dialog");
    this.dialog.showDialog();
    // this.getUserLocation();
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight
    })
    const self = this
    wx.loadFontFace({
      family: 'fonts',
      source: 'url("https://test.jwsxmall.com/jiweishengxian/VisbyCF-Bold.otf")',
      success(res) {
        console.log(res.status)
        self.setData({ loaded: true })
      },
      fail: function (res) {
        console.log(res.status)
      },
      complete: function (res) {
        console.log(res.status)
      }
    });
  },
  /**
   * 页面分享
   */
  // onShareAppMessage: function () {
  //   var that = this;
  //   return {
  //     title: that.data.title,
  //     desc: '分享页面的内容',
  //     imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
  //     path: '/pages/homePage/homePage?partnerUserId=' + wx.getStorageSync("userId") + '&type=2', // 路径，传递参数到指定页面
  //   }
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    /**
     * 购物车数量
     */
    wx.request({
      url: app.globalData.url + "countShoppingCartNum",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId"),
      },
      success: function (res) {
        var data = res.data.data;
        that.setData({
          cartCount: data
        })
      }
    });
    //商城首页banner
    wx.request({
      url: app.globalData.url + "getAdvertisementListByType?advertisingType=5",
      method: "GET",
      data: {
      },
      success: function (res) {
        var data = res.data.data;
        if (res.data.result == 1) {
          that.setData({
            mallBanner: data,
          })
        }
      }
    }),
      /**
       * 产品推荐
       */
      wx.request({
        url: app.globalData.url + "getProductRecommendList",
        method: "GET",
        data: {
          userId:wx.getStorageSync("userId")
        },
        success: function (res) {
          var data = res.data.data;
          if (res.data.result == 1) {
            that.setData({
              productRecommend: data
            })
          }
        }
      });
      /**
       * 获取有效的优惠卷
       */
        wx.request({
          url: app.globalData.url + "getValidCouponListAll",
          method: "GET",
          data: {
            userId:wx.getStorageSync("userId")
          },
          success: function (res) {
            var data = res.data.data;
            if (res.data.result == 1) {
              that.setData({
                validCoupon: []
              })
              if (res.data.result == 1) {
                if (data.length > 3) {
                  for (var i = 0; i < 3; i++) {
                    that.data.validCoupon.push(data[i])
                  }
                } else {
                  for (var i = 0; i < data.length; i++) {
                    that.data.validCoupon.push(data[i])
                  }
                }
                that.setData({
                  validCoupon: that.data.validCoupon
                })
                console.info(that.data.validCoupon);
              }
            }
          }
        });
      /**
       * 获取秒杀
       */
      wx.request({
        url: app.globalData.url + "getSkillProduct",
        method: "GET",
        data: {
        },
        success: function (res) {
          var data = res.data.data;
          that.setData({
            categoryTags: []
          })
          if (res.data.result == 1) {
            if (data.length > 3) {
              for (var i = 0; i < 3; i++) {
                that.data.categoryTags.push(data[i])
              }
            } else {
              for (var i = 0; i < data.length; i++) {
                that.data.categoryTags.push(data[i])
              }
            }
            console.info(that.data.categoryTags);
            that.setData({
              categoryTags: that.data.categoryTags
            })
            console.info(that.data.categoryTags);
          }
        }
      });
      /**
       * 获取预售
       */
      wx.request({
        url: app.globalData.url + "getAdvanceSaleProduct",
        method: "GET",
        data: {
          userId: wx.getStorageSync("userId")
        },
        success: function (res) {
          var data = res.data.data;
          that.setData({
            advanceSale: []
          })
          if (res.data.result == 1) {
            if (data.length > 7) {
              for (var i = 0; i < 7; i++) {
                that.data.advanceSale.push(data[i])
              }
            } else {
              for (var i = 0; i < data.length; i++) {
                that.data.advanceSale.push(data[i])
              }
            }
            console.info(that.data.advanceSale);
            that.setData({
              advanceSale: that.data.advanceSale
            })
            console.info(that.data.advanceSale);
          }
        }
      });
    this.getPtp();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 跳转到产品详情
   */
  details: function (res) {
    var productid = res.currentTarget.dataset.productid;
    if (res.currentTarget.dataset.type == 2) {
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?productid=' + productid + '&type=2',
      })
    } else {
      console.info(66666);
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?productid=' + productid + '&yushou=1&consignmenttimestr=' + res.currentTarget.dataset.consignmenttimestr + '&endtimestr=' + res.currentTarget.dataset.endtimestr+"&type=5",
      })
    }
   

  },

  /**
   * 跳转到秒杀产品详情
   */
  detailSeckill: function (res) {
    var productid = res.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + productid ,
    })

  },
  /**
   * 跳转到产品分类
   */
  goClass: function (res) {
    var type = res.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/productParentType/productParentType?productType=' + type,
    })

  },
  cart: function (e) {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var productid = e.currentTarget.dataset.productid
    wx.request({
      url: app.globalData.url + "modifyShoppingCart",
      method: "GET",
      data: {
        userId: userId,
        num: 1,
        productId: productid
      },
      success: function (res) {
        var data = res.data.data;
        if (res.data.result == 1) {
          that.setData({
            productType: data
          })
        }
      }
    });
  },
  classification: function (res) {
    var that = this;
    var typeid = res.currentTarget.dataset.typeid;
    console.info(typeid);
    wx.navigateTo({
      url: '/pages/productListNew/productListNew?typeId=' + typeid,
    })
  },
  more: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/productCategories/productCategories',
    })
  },
  moreAndMore:function(){
    wx.navigateTo({
      url: '/pages/couponCenter/couponCenter',
    })
  },
  discount: function () {
    wx.navigateTo({
      url: '/pages/discount/discount',
    })
  },
  seckill: function () {
    wx.navigateTo({
      url: '/pages/seckill/seckill',
    })
  },
  chou: function () {
    wx.navigateTo({
      url: '/pages/chou/chou',
    })
  },
  // 立即购买\加入购物车
  immeBuy(e) {
    var that = this;
    wx.request({
      url: app.globalData.url + "modifyShoppingCart",//给函数传递服务器地址参数 
      data: {
        productId: e.currentTarget.dataset.productid,
        userId: wx.getStorageSync("userId"),
        num: 1,
      },//给服务器传递数据，本次请求不需要数据，可以不填
      header: {
        'content-type': 'application/json' // 默认值，返回的数据设置为json数组格式
      },
      success: function () {//打印出返回的数据
        //that.toCar();
        // that.toCar();
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000
        });
        /**
         * 购物车数量
         */
        wx.request({
          url: app.globalData.url + "countShoppingCartNum",
          method: "GET",
          data: {
            userId: wx.getStorageSync("userId"),
          },
          success: function (res) {
            var data = res.data.data;
            that.setData({
              cartCount: data
            })
          }
        });
      }
    })
  },
  preferential: function () {
    wx.navigateTo({
      url: '/pages/couponCenter/couponCenter',
    })
  },
  tofabu: (event) => {
    wx.navigateTo({
      url: '/pages/shoppingCart/shoppingCart',
    })
  },
  mentionCarPresent:function(){
    wx.navigateTo({
      url: '/pages/mentionCarPresent/mentionCarPresent',
    })
  },
  getScancode: function () {
    var _this = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        var result = res.result;

        _this.setData({
          result: result,

        })
      }
    })
  },
  goSeckill:function(){
    wx.navigateTo({
      url: '/pages/seckill/seckill',
    })
  },
  goPresell: function () {
    wx.navigateTo({
      url: '/pages/presell/presell',
    })
  },
  receive:function(e){
    wx.request({
      url: app.globalData.url + "userReceiveCoupon",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId"),
        couponId: e.currentTarget.dataset.couponid,
      },
      success: function (res) {
        if (res.data.result == 1){
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 2000
          });
          /**
           * 获取有效的优惠卷
           */
          wx.request({
            url: app.globalData.url + "getValidCouponListAll",
            method: "GET",
            data: {
              userId: wx.getStorageSync("userId")
            },
            success: function (res) {
              var data = res.data.data;
              if (res.data.result == 1) {
                that.setData({
                  validCoupon: []
                })
                if (res.data.result == 1) {
                  if (data.length > 3) {
                    for (var i = 0; i < 3; i++) {
                      that.data.validCoupon.push(data[i])
                    }
                  } else {
                    for (var i = 0; i < data.length; i++) {
                      that.data.validCoupon.push(data[i])
                    }
                  }
                  that.setData({
                    validCoupon: that.data.validCoupon
                  })
                  console.info(that.data.validCoupon);
                }
              }
            }
          });
        }
      }
    });
  },
  goGroup:function(){
    wx.navigateTo({
      url: '/pages/spellGroup/spellGroup',
    })
  },
  getPtp: function () {//拼团商品推荐
    var that = this;
    wx.request({
      url: app.globalData.url + 'getGroupShoppingProduct',
      data: {
        userId:wx.getStorageSync("userId")
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var result = new Array();
        for(var i=0;i<res.data.data.length;i++){
          console.log(res.data.data[i].virtualSalesVolume)
          result[i] = (res.data.data[i].virtualSalesVolume/ res.data.data[i].inventoryQuantity).toFixed(2)*100;
        }
        that.setData({
          pintuanPro: res.data.data,
          result:result
        })
      },
      fail: function (err) {
      },
    })
  },
  toIntegralMall:function(){
    wx.navigateTo({
      url: '/pages/integralMall/integralMall',
    })
  },
  checkIn: function () {
      wx.navigateTo({
        url: '/pages/checkIn/checkIn',
      })
    },
  invitation: function () {
    wx.navigateTo({
      url: '/pages/invitation/invitation',
    })
  },
})